package com.example.weiwing.home.dynamic;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.weiwing.R;
import com.example.weiwing.config.WeiWingConfig;
import com.example.weiwing.entity.Article;
import com.squareup.picasso.Picasso;

import java.text.DateFormat;
import java.util.List;

;

public class DynamicAdapter extends RecyclerView.Adapter<DynamicAdapter.ViewHolder> {

    private List<Article> articleList;

    private Context context;

    public DynamicAdapter(Context context,List<Article> articleList) {
        this.context = context;
        this.articleList = articleList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Article article = articleList.get(position);

        holder.contentTextView.setText(article.getTitle());

        // TODO: Glide 和 Picasso 加载网络图片
        // 使用 Glide 加载网络图片
//        Glide.with(holder.itemView.getContext())
//                .load(article.getImgUrl())
//                .into(holder.imageView);
//        holder.userNameTextView.setText(article.getNickName());
        // 使用 Glide 加载头像图片
//        Glide.with(holder.itemView.getContext())
//                .load(article.getHeadImgUrl())
//                .into(holder.headImageView);
//        new LoadPictureThread(holder.imageView, article.getImgUrl()).start();
//        new LoadPictureThread(holder.headImageView, article.getUser().getAvatar()).start();
        Picasso.get().load(WeiWingConfig.getUrl() + article.getImgUrl()).into(holder.imageView);
        Picasso.get().load(WeiWingConfig.getUrl() + article.getUser().getAvatar()).into(holder.headImageView);

        holder.userNameTextView.setText(article.getNickName());
        holder.likeCountTextView.setText(String.valueOf(article.getLikes()));
        holder.commentCountTextView.setText(String.valueOf(article.getComments()));
        holder.shareCountTextView.setText(String.valueOf(article.getReposts()));
        holder.timePostedTextView.setText(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.DEFAULT).format(article.getCreateTime()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(view.getContext(), DynamicView.class);
                it.putExtra("articleId", String.valueOf(article.getArticleId()));
                view.getContext().startActivity(it);
            }
        });
    }

    @Override
    public int getItemCount() {
        return articleList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView contentTextView;
        public ImageView imageView; // 文章图片
        public ImageView headImageView; // 用户头像
        public TextView userNameTextView;
        public ImageView profilePicImageView;
        public TextView likeCountTextView;
        public TextView commentCountTextView;
        public TextView shareCountTextView;
        public TextView timePostedTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            contentTextView = itemView.findViewById(R.id.textView_dynamic_content);
            imageView = itemView.findViewById(R.id.imageView_dynamic_pic);
            headImageView = itemView.findViewById(R.id.imageView_profile_pic);
            userNameTextView = itemView.findViewById(R.id.textView_user_name);
            profilePicImageView = itemView.findViewById(R.id.imageView_profile_pic);
            likeCountTextView = itemView.findViewById(R.id.textView_like_count);
            commentCountTextView = itemView.findViewById(R.id.textView_comment_count);
            shareCountTextView = itemView.findViewById(R.id.textView_share_count);
            timePostedTextView = itemView.findViewById(R.id.textView_time_posted);
        }
    }

}
